package com.siwei.mes.service.erp.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.erp.Itemorder;
import com.siwei.mes.entity.experiment.ItemorderExtend;
import com.siwei.mes.entity.materials.Materials;
import com.siwei.mes.mapper.ItemorderExtendMapper;
import com.siwei.mes.mapper.erp.ItemorderMapper;
import com.siwei.mes.mapper.materials.MaterialsMapper;
import com.siwei.mes.service.erp.ItemorderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * ERP-小票(Itemorder)表服务实现类
 *
 * @author linzi
 * @since 2024-07-29 16:13:08
 */
@Service
public class ItemorderServiceImpl implements ItemorderService {
    @Resource
    private ItemorderMapper itemorderMapper;
    @Resource
    private MaterialsMapper materialsMapper;
    @Resource
    private ItemorderExtendMapper itemorderExtendMapper;

    @Override
    public PageInfo<Itemorder> getPage(PageQuery<Itemorder, Itemorder> query) {
        Itemorder searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> itemorderMapper.getList(searchParam));
    }

    @Override
    public Itemorder loadByItId(Integer ItId) {
        Itemorder itemorder = itemorderMapper.loadByItId(ItId);
        this.payloadItemorderExtendInfo(itemorder);
        return itemorder;
    }

    /**
     * 组装品控小票扩展信息
     *
     * @param itemorder
     */
    private void payloadItemorderExtendInfo(Itemorder itemorder) {
        if (itemorder == null) {
            return;
        }
        ItemorderExtend itemorderExtend = itemorderExtendMapper.selectByItemorderNo(itemorder.getItemorderno());
        if (itemorderExtend == null) {
            itemorderExtend = new ItemorderExtend();
            itemorderExtend.setItid(itemorder.getItid());
            itemorderExtend.setItemorderno(itemorder.getItemorderno());
            itemorderExtend.setCreateTime(new Date());
            itemorderExtend.setUpdateTime(new Date());
            /**这里赋值小票的代表数量*/
            float behalfNumber = 0;
            float scquantity = itemorder.getScquantity() == null ? 0 : itemorder.getScquantity();
            float fhquantity = itemorder.getFhquantity() == null ? 0 : itemorder.getFhquantity();
            /**
             * 当每次小票完成时，如生产方量为0，发货方量大于0的情况下，则找到字段：上一张小票字段
             * 小票扩展表新增字段：代表数量
             * 当生产方量不为0时，生产方量和代表方量为一致
             * 当生产方量为0时，则当前小票的发货方量=代表方量，当前小票的上一张小票的代表数量-本小票的代表方量
             */
            if (scquantity > 0) {
                behalfNumber = scquantity;
            } else {
                behalfNumber = fhquantity;
            }
            itemorderExtend.setBehalfNumber(behalfNumber);
            itemorderExtendMapper.insertSelective(itemorderExtend);
        }
        itemorder.setItemorderExtend(itemorderExtend);
    }

    @Override
    public Itemorder loadByItemorderNo(String itemorderNo) {
        Itemorder itemorder = itemorderMapper.selectByItmeorderNo(itemorderNo);
        this.payloadItemorderExtendInfo(itemorder);
        return itemorder;
    }

    @Override
    public Itemorder loadByItIdAndCreateFromItemoderNo(Integer itemorderId, String fromItemoderNo) {
        Itemorder itemorder = this.loadByItId(itemorderId);
        if (itemorder != null && itemorder.getItemorderExtend() != null && StringUtils.isNotBlank(fromItemoderNo)) {
            ItemorderExtend itemorderExtend = itemorder.getItemorderExtend();
            itemorderExtend.setFromItemorderNo(fromItemoderNo);
            itemorderExtend.setUpdateTime(new Date());
            itemorderExtendMapper.updateByPrimaryKeySelective(itemorderExtend);

            itemorder.setFromItemoderNo(fromItemoderNo);
            itemorder.setItemorderExtend(itemorderExtend);
        }
        return itemorder;
    }

    @Override
    public List<Itemorder> selectItidList(List<Integer> itemOrderIds) {
        List<Itemorder> itemorderList = itemorderMapper.selectItidList(itemOrderIds);
        for (Itemorder itemorder : itemorderList) {
            this.payloadItemorderExtendInfo(itemorder);
        }
        return itemorderList;
    }

    @Override
    public List<Itemorder> getAll(Itemorder entity) {
        return itemorderMapper.getList(entity);
    }

    @Override
    public PageInfo<Itemorder> bindPage(PageQuery<Itemorder, Itemorder> query) {
        Itemorder searchParam = query.getParams();
        if (searchParam.getExperimentId() != null) {
            Materials materials = materialsMapper.getMaterialsByExperimentId(searchParam.getExperimentId());
            if (materials != null) {
                searchParam.setTpz(materials.getMaterialName());
            }
        }
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> itemorderMapper.bindList(searchParam));
    }
}
